<?php
/*
 * Created on 22.04.2008
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
 class Controller_Catalog extends Controller_Base_Admin{

	protected $goods;
	private $size;
	private $color; 

	function __construct() {
		parent::__construct();
		
//		$g = Model_Good();
//print_r($g);
		
		$this->goods	= new Model_Good();
		$this->size 	= new Model_Size();
		$this->color	= new Model_Color();
		$this->structure= new Model_Structure(); 
		
	}

 	
 	function index($args) {
 		$template = new Template('admin/index.tmpl');
 		
 		$sigs = array(
	 		'cid'	=> array('type' => 'int'),
 		);
 		
 		
// 		print_r($args);
 		
 		sanitize_vars($_GET,$sigs,null);
 		
// 		$goods->find(array(category => $_GET['cid']));
 		
 		if($_POST[action]==='rests') {
// 			print_r($_FILES);
 			
 			if(!empty($_FILES['excel']['error'])) {
 				error_log($_FILES['excel']['error']);
 			} elseif(empty($_FILES['excel']['tmp_name']) || $_FILES['excel']['tmp_name'] == 'none') {
				error_log('No file was uploaded..');
 			} else {
 				
 				$file = site_path.'tmp/'.$_FILES['excel']['name'];
 				copy($_FILES['excel']['tmp_name'], $file);
 				unlink ($_FILES['excel']['tmp_name']);
 				require_once site_path.'libs/Spreadsheet/Excel/reader.php';
 				// ExcelFile($filename, $encoding);
				$data = new Spreadsheet_Excel_Reader();

				// Set output Encoding.
				$data->setOutputEncoding('CP1251');
 				
 				$data->read($file);
 			
 				$this->goods->loadRests($data->sheets[0][cells]);
 			}
 			
 			return array(
 				status 		=> 302,
 				location	=> '/admin/catalog'
 			);
 		}
 		
		
// 		print "<pre>";
// 		print_r($this->structure->getList());
// 		print "</pre>";
// 		
 		$template->setArgs(array(
 			tabs 		=> $this->tabs->get('catalog'),
 			menu		=> $this->menu->getHierarchicalMenu(array(uid => $this->admin->id)),
 			
 			goods		=> $this->goods->find($_GET['all'] ? array(sex => $args[0]) : array(sex => $args[0] === 'male' ? 1 : 0 , category => $_GET['cid'])),
 			categories 	=> $this->goods->getCategories($args[0] === 'male' ? 1 : 0),
 			sizes		=> $this->size->getList(),
 			colors		=> $this->color->getList(),
 			structures	=> $this->structure->getList(),
 			cid			=> $_GET['cid'],
 			_user		=> $this->admin,
 			sex 		=> $args[0],
 		));
		 		
 		return array(
 			status 		=> 0,
 			template	=> $template,
 		);

 	}
 	
 	function male($args) {
 		$action = $args[0] ? $args[0] : 'index';
 		return $this->$action(array_merge(array('male'), $args));
 	}
 	
 	function female($args) {
 		$action = $args[0] ? $args[0] : 'index';
 		return $this->$action(array_merge(array('female'), $args));
 	} 	
 	
 	function add($args){
 		$template = new Template('admin/index.tmpl');

		
		if($_POST[action] === 'add') {

//print "<pre>";
//print_r($_POST);
//print "</pre>";			
			$sigs = array(
	 			name		=> array('type' => 'string'),
	 			description	=> array('type' => 'string','function'=>array('stripslashes'),),
	 			code		=> array('type' => 'string'),
	 			price		=> array('type' => 'string'),
	 			size		=> array('type' => 'array'),
	 			color		=> array('type' => 'array'),
	 			sex			=> array('type' => 'bool'),
	 			exists		=> array('type' => 'bool'),
	 			category	=> array('type' => 'int'),
	 			structure	=> array('type' => 'int'),
	 			exists		=> array('type' => 'bool')
 			);
 			
 			sanitize_vars($_POST,$sigs, null);
						
			try{
				$this->goods->add($_POST);
			} catch (GoodsException $e) {
				$template->setArgs(array(
					goods_error =>	$e->getMessage(),
					name		=> $_POST[name],
					description	=> array('type' => 'string','function' => array('stripslashes')),
					code		=> $_POST[code],
					price		=> $_POST[price],
					size		=> $_POST[size],
					color		=> $_POST[color],
					sex			=> $_POST[sex],
					exists		=> $_POST[exists]
				));
				error_log($e->getMessage());			
			} 
		
			if(!isset($e))
				$template->setArg(success,1);	
		
			
			
		}
		
		
		
 		$template->setArgs(array(
 			tabs 		=> $this->tabs->get('catalog_add'),
 			menu		=> $this->menu->getHierarchicalMenu(array(uid => $this->admin->id)), 
 			categories 	=> $this->goods->getCategories($args[0] === 'male' ? 1 : 0 ),	
 			
 			sizes		=> $this->size->getList(),	
 			colors		=> $this->color->getList(), 
 			structures	=> $this->structure->getList(),
 			sex			=> $args[0],
 			exists		=> 1
 		)); 		
 		
 		
 		return array(
 			status 		=> 0,
 			template	=> $template,
 		);
 	}
 	
 	function delete($args) {
 		$this->goods->delete($_GET[id]);
 		
 		return array(
 			status 		=> 302,
 			location	=> '/admin/catalog/'
 		);
 	}
 	
 	function edit($args) {
 		$template = new Template('admin/index.tmpl');

		if($_POST[action] === 'edit') {
			
			$sigs = array(
				id			=> array('type'	=> 'int'),
	 			name		=> array('type' => 'string'),
	 			category	=> array('type'	=> 'int'),
	 			description	=> array('type' => 'string','function' => array('stripslashes')),
	 			code		=> array('type' => 'string'),
	 			price		=> array('type' => 'string'),
	 			size		=> array('type' => 'array'),
	 			color		=> array('type' => 'array'),
	 			sex			=> array('type' => 'bool'),
	 			exists		=> array('type' => 'bool'),
	 			structure	=> array('type' => 'int'),
 			);
 			
 			sanitize_vars($_POST,$sigs, null);
		
			try{
				$this->goods->save($_POST);
			} catch(GoodsException $e) {
				error_log($e->getMessage());
				$template->setArgs(array(
					goods_error =>	$e->getMessage(),
				));				
			}
			
			if(!$e)
				return array(
					status 		=> 302,
					location	=> '/admin/catalog/'
				);
				
			$template->setArgs(array(	 			
	 			id			=> $_POST[id],
	 			name		=> $_POST[name],
	 			description	=> $_POST[description],
	 			code		=> $_POST[code],
	 			price		=> $_POST[price],
	 			category	=> $_POST[category],
	 			sex			=> $_POST[sex],
	 			exists		=> $_POST[exists],
	 			structure	=> $_POST[structure],
	 			
	 			sizes		=> $this->size->getList(),	
	 			colors		=> $this->color->getList(),
	 			structures	=> $this->structure->getList(), 
	 		));			
			
		} else {
			$item  = $this->goods->find(array(id => $_GET[id]));
			
//			$structure 	= array();
			$size		= array();
			$color		= array();
			
			foreach($this->size->getListByMask($item->size) as $s) {
				$size[] = $s[description];				
			}	
	 		
			foreach($this->color->getListByMask($item->color) as $c) {
				$color[] = $c[description];				
			}	
	 		
//	 		foreach($this->structure->getListByMask($item->structure) as $st) {
//				$structure[] = $st[description];				
//			}
	 		
	 		$template->setArgs(array(
	
	 			id			=> $item->id,
	 			name		=> $item->name,
	 			description	=> $item->description,
	 			code		=> $item->art,
	 			price		=> $item->price,
	 			category	=> $item->category,
	 			sex			=> $item->sex,
	 			exists		=> $item->exists,
	 			structure	=> $item->structure,
	 			
	 			size		=> join(',',$size),
	 			sizes		=> $this->size->getList(),
	 			
	 			color		=> join(',',$color),
	 			colors		=> $this->color->getList(),
	 			
	 			structures	=> $this->structure->getList(),
	 			structure	=> $item->structure,
	 			
	 		)); 			
		}

 		$template->setArgs(array(
 			tabs 		=> $this->tabs->get('catalog_edit'),
 			menu		=> $this->menu->getHierarchicalMenu(array(uid => $this->admin->id)), 
 			categories 	=> $this->goods->getCategories($args[0] === 'male' ? 1 : 0),			
		));

  		return array(
 			status 		=> 0,
 			template	=> $template,
 		);		
 	}
 	
 	function category($args) {
 		$template = new Template('admin/index.tmpl');

		if($_POST[action] === 'add') {

			$this->goods->addCategory($_POST[name],$_POST[sex]);

			return array(
				status 		=> 302,
				location	=> '/admin/catalog/'.$args[0].'/category/'
			);
		}
		
		
		if($_GET[delete]) {

			$this->goods->deleteCategory($_GET[delete]);
			
		}

 		$template->setArgs(array(
 			tabs 		=> $this->tabs->get('categories'),
 			menu		=> $this->menu->getHierarchicalMenu(array(uid => $this->admin->id)), 
 			sex			=> $args[0],
 			categories 	=> $this->goods->getCategories($args[0] === 'male' ? 1 : 0),
 				
 		));

  		return array(
 			status 		=> 0,
 			template	=> $template,
 		);
 	}
 	
 	function goods_manage($args) {
 		$template = new Template('admin/index.tmpl'); 		

		if($_POST[action] === 'save') {

			$sigs = array(
				id			=> array('type'	=> 'int'),
	 			picture		=> array('type' => 'string','function' => array('stripslashes')),
				description	=> array('type' => 'string','function' => array('stripslashes')),
 			);
 			
 			sanitize_vars($_POST,$sigs, null);

			$this->color->save($_POST['picture'],$_POST['description'],$_POST[id]);
			
			return array(
				status 		=> 302,
				location 	=> '/admin/catalog/#catalog-setup'
			);			
		}
		
		if($_POST[action] === 'add') {
			
			if($_POST[type] === 'color') {
				$id = $this->color->add($_POST['description']);	
			} else {
				$id = $this->structure->add($_POST['description']);
			}
			
			error_log("NEW COLOR id: $id");

			return array(
				status 		=> 302,
				location 	=> '/admin/catalog/#catalog-setup'
			);	
			
//			return array(
//				status 		=> 302,
//				location 	=> '/admin/catalog/goods_manage?object=color&item='.$id
//			);
		}
		

 		$template->setArgs(array(
 			
  			tabs 		=> $this->tabs->get('manage'),
 			menu		=> $this->menu->getHierarchicalMenu(array(uid => $this->admin->id)), 
 			categories 	=> $this->goods->getCategories(),
 			object		=> $_GET[object],
 			item		=> $this->color->find(array(id => $_GET[item])),	
 		));
 		 			
  		return array(
 			status 		=> 0,
 			template	=> $template,
 		); 		
 	}
 	
 	function suits($args) {
 		$template = new Template('admin/index.tmpl'); 	
 		$suits	= new Model_Suit();
  		
  		if($_POST[add] === 'suit' &&  $_POST[name]) {
  			$suits->add($_POST[name]);
  		} else if ($_POST[add] === 'good') {
  			$suits->addGood($_POST[good_id],$_POST[id]);
  			
  			return array(
  				status 		=> 302,
  				location	=> '/admin/catalog/suits/'.$_POST[id]
  			);
  		}
  		
  		if($_GET[delete] === 'suit') {
  			$suits->delete($_GET[id]);
  		} else if ($_GET[delete] === 'good') {
  			$suits->deleteGood($_GET[id],$_GET[good_id]);
  			
  			return array(
  				status 		=> 302,
  				location	=> '/admin/catalog/suits/'.$_GET[id]
  			);  			
  		}
  		
 		if(isset($args[0])) {
 			$goods = new Model_Good();	
 		
			$template->setArgs(array(
	 			suit		=> $suits->find(array(id => $args[0])),
	 			goods		=> $goods->find(),
	 			id			=> $args[0]
	 		)); 	 			
 		} else {
			$template->setArgs(array(
	 			suits		=> $suits->find(),
	 		)); 			
 		}
 	 		
 		$template->setArgs(array(
 			tabs 		=> $this->tabs->get('suits'),
 			menu		=> $this->menu->getHierarchicalMenu(array(uid => $this->admin->id)),

 			_user		=> $this->admin
 		));
 	
  		return array(
 			status 		=> 0,
 			template	=> $template,
 		); 
 	}
 }
 
?>
